package com.sky.service.admin.impl;

import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.user.OrdersMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //今日数据
    @Override
    public BusinessDataVO businessDate(LocalDate begin,LocalDate end) {
        Integer save = userMapper.selectSaveByToday(begin,end);
        if (null == save) {
            save = 0;
        }
        log.info("SQL今日用户数,当前时间-----{},{},{}", save, begin,end);

        Map<String, Object> turnoverAndUnitPriceMap = ordersMapper.selectOrdersNumByToday(begin,end);
        Object turnover;
        Object unitPrice;
        if (null == turnoverAndUnitPriceMap) {
            turnover = 0.0;
            unitPrice = 0.0;
        } else {
            turnover = turnoverAndUnitPriceMap.get("sum");
            if (null == turnover) {
                turnover = 0.0;
            }
            unitPrice = turnoverAndUnitPriceMap.get("avg");
            if (null == unitPrice) {
                unitPrice = 0.0;
            }
        }
        log.info("营业额和平均客单价====={},{}", turnover, unitPrice);
        Integer validOrderCount = ordersMapper.selectValidOrders(begin,end);
        if (null == validOrderCount) {
            validOrderCount = 0;
        }
        log.info("有效订单数====={}", validOrderCount);
        Double orderCompletionRate = ordersMapper.selectEffectiveOrder(validOrderCount,begin,end);
        if (null==orderCompletionRate){
            orderCompletionRate=0.0;
        }
        log.info("订单完成率====={}", orderCompletionRate);
        BusinessDataVO vo = BusinessDataVO.builder()
                .turnover(Double.parseDouble(turnover.toString()))
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(Double.parseDouble(unitPrice.toString()))
                .newUsers(save)
                .build();
        return vo;
    }

    //订单管理
    @Override
    public OrderOverViewVO overviewOrders() {
        int allOrders = ordersMapper.selectCountOrStatus(LocalDate.now(), LocalDate.now(), null);
        Map<String, Object> map = ordersMapper.selectCountByStatus(LocalDate.now());
        //待接单
        Integer waitingOrders = Integer.parseInt(map.get("2").toString());
        //待派送
        Integer deliveredOrders = Integer.parseInt(map.get("3").toString()) + Integer.parseInt(map.get("4").toString()) + Integer.parseInt(map.get("5").toString());
        //已完成
        Integer completedOrders = Integer.parseInt(map.get("6").toString());
        //已取消
        Integer cancelledOrders = Integer.parseInt(map.get("7").toString());
        OrderOverViewVO vo = OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .build();
        return vo;
    }

    //菜品总览
    @Override
    public SetmealOverViewVO overviewDishes() {
        Map<String, Object> map = dishMapper.selectBystatus();
        Integer sold = Integer.parseInt(map.get("1").toString());
        Integer discontinued =Integer.parseInt(map.get("0").toString());
        SetmealOverViewVO vo = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return vo;
    }
    //套餐总览
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map<String, Object> map = setmealMapper.selectBystatus();
        Integer sold = Integer.parseInt(map.get("1").toString());
        Integer discontinued =Integer.parseInt(map.get("0").toString());
        SetmealOverViewVO vo = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
        return vo;
    }
}
